package com.example.woke1;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebSettings.TextSize;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.woke.ZhouchougongyiActivity;
import com.http.HttpMethods;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.netease.nim.uikit.GlideApp;
import com.woke.data.Datas_chouinfo;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import cn.xl.xxl.imagecache.ImageLoader;

public class ZhongchouinfoActivity extends Activity implements OnClickListener {

	private Intent intent;
	private String id;//��Ʒid
	private TextView mTpercent, mThasmoney, mTok, mTpname, mTminmax;
	private ImageView mItop;
	private WebView mWebView;
	private ProgressBar mBar;
	private ImageLoader imageLoader;
	private String myzhong;
	private View findViewById;
	private String type;//3���ڳ������
	private Datas_chouinfo jsonObject;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate (savedInstanceState);
		setContentView (R.layout.activity_zhongchouinfo);
		imageLoader = new ImageLoader (this);
		intent = getIntent ();
		intview ();

	}

	@Override
	protected void onDestroy() {
		imageLoader.clearCache ();
		imageLoader.clearMemoryCache ();
		super.onDestroy ();
	}

	@SuppressLint("JavascriptInterface")
	private void intview() {
		findViewById (R.id.avzhongchouinfo_image_back).setOnClickListener (new OnClickListener () {

			@Override
			public void onClick(View v) {
				finish ();
			}
		});
		id = intent.getStringExtra ("id");
		myzhong = intent.getStringExtra ("myzhong");//�ж��Ƿ�Ҫ��ʾ����Ҫ֧�֡�
		mTpercent = (TextView) findViewById (R.id.zhongchouinfo_text_percent);//�Ｏ�����
		mThasmoney = (TextView) findViewById (R.id.zhongchouinfo_text_hasmoney);//�ѳ��
		mWebView = (WebView) findViewById (R.id.zhongchouinfo_text_info);//��Ŀ����
		mTok = (TextView) findViewById (R.id.zhongchouinfo_text_ok);//��Ҫ����
		if (myzhong.equals ("1")) {
			mTok.setText ("已付款，等待发货");
		}
		if (myzhong.equals ("2")) {
			mTok.setText ("确认收货");
		}
		if (myzhong.equals ("3")) {
			mTok.setText ("已完成");
		}
		if (myzhong.equals ("4")) {
			mTok.setVisibility (View.GONE);
		}
		mTpname = (TextView) findViewById (R.id.zhongchouinfo_text_pname);//��Ŀ���
		mTminmax = (TextView) findViewById (R.id.zhongchouinfo_text_minmax);//������
		mItop = (ImageView) findViewById (R.id.zhongchouinfo_image_top);
		mBar = (ProgressBar) findViewById (R.id.zchouifno_progress);
		findViewById = findViewById (R.id.zhongchouinfo_text_morelist);
		findViewById.setOnClickListener (this);
		mTok.setOnClickListener (this);
		getdatainfo ();
//		mWebView = (WebView) findViewById(R.id.webView13);
		WebSettings settings = mWebView.getSettings ();
		setSettings (settings);

//		mWebView.addJavascriptInterface (new JavascriptInterface (
//				getApplicationContext ()), "imagelistner");
		mWebView.setWebChromeClient (new MyWebChromeClient ());
		mWebView.setWebViewClient (new MyWebViewClient ());
//		mWebView.loadUrl("http://www.88china.com");
		mWebView.loadUrl (HttpMethods.BASEURL + "g=api&m=crowd&a=xmjs&id=" + id);
	}

	private void getdatainfo() {
		AsyncHttpClient ahc = new AsyncHttpClient ();
		RequestParams params = new RequestParams ();
		params.put ("g", "api");
		params.put ("m", "crowd");
		params.put ("a", "crowdDetail");
		params.put ("crowdid", id);
		ahc.get (HttpMethods.BASEURL, params, new JsonHttpResponseHandler () {

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess (statusCode, headers, response);

				Log.e ("详情", "" + response);
				try {
					String flag = response.getString ("flag");
					String info = response.getString ("info");
					if (flag.equals ("success")) {
						JSONObject object = response.getJSONObject ("data");
						String string = object.getString ("data");
						jsonObject = com.alibaba.fastjson.JSONObject.parseObject (string, Datas_chouinfo.class);

						mBar.setProgress (jsonObject.getPercent () * 100);
						mTpercent.setText (jsonObject.percent * 100 + "%");
						mThasmoney.setText ("已筹款：" + jsonObject.getTrue_money () + "元");
						type = jsonObject.getType ();
//							mTinfo.setText(product_content);
//							mTinfo.getSettings().setDefaultTextEncodingName("UTF-8") ;
//							mTinfo.loadDataWithBaseURL(Contants.BASEURL, product_content, "text/html", "UTF-8", null);
						mTpname.setText ("  " + jsonObject.getProduct_name ());
						if (jsonObject.getType ().equals ("3")) {
							mTminmax.setText ("￥" + jsonObject.getMoney ());
							findViewById.setVisibility (View.GONE);
						} else {
							mTminmax.setText ("￥" + jsonObject.getMin () + "——" + "￥" + jsonObject.getMax ());
						}
						if (jsonObject.getProduct_img ().startsWith ("http")) {
							GlideApp.with (ZhongchouinfoActivity.this)
									.load (jsonObject.getProduct_img ())
									.placeholder(R.mipmap.icn__launch_oem)
									.error(R.mipmap.icn__launch_oem)
									.into (mItop);
						} else {
							GlideApp.with (ZhongchouinfoActivity.this)
									.load ("http://" + jsonObject.getProduct_img ())
									.placeholder(R.mipmap.icn__launch_oem)
									.error(R.mipmap.icn__launch_oem)
									.into (mItop);
						}
//							imageLoader.DisplayImage("http://"+jsonObject.getProduct_img(), mItop, false, false, true,4);
					} else {
						Toast.makeText (ZhongchouinfoActivity.this, "获取数据失败！", Toast.LENGTH_SHORT)
								.show ();
					}
				} catch (JSONException e) {
					e.printStackTrace ();
				}
			}

			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart ();
			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure (statusCode, headers, responseString, throwable);
				Log.e ("response", "ʧ��" + responseString);
			}

			@Override
			public void onFinish() {
				super.onFinish ();
//				Log.e("response", "���");
			}
		});
	}

	private void shouhuo() {
		AsyncHttpClient ahc = new AsyncHttpClient ();
		RequestParams params = new RequestParams ();
		params.put ("g", "api");
		params.put ("m", "user");
		params.put ("a", "takedelivery");
		params.put ("id", id);
		ahc.get (HttpMethods.BASEURL, params, new JsonHttpResponseHandler () {
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess (statusCode, headers, response);
				Log.e ("确认收货", "" + response);
				try {
					String flag = response.getString ("flag");
					String string = response.getString ("info");
					if (flag.equals ("success")) {
						Toast.makeText (ZhongchouinfoActivity.this, "" + string, Toast.LENGTH_SHORT)
								.show ();
					} else {
						Toast.makeText (ZhongchouinfoActivity.this, "" + string, Toast.LENGTH_SHORT)
								.show ();
					}
				} catch (JSONException e) {
					e.printStackTrace ();
				}
			}

			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart ();
			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure (statusCode, headers, responseString, throwable);
				Toast.makeText (ZhongchouinfoActivity.this, "服务器未响应!", Toast.LENGTH_SHORT).show ();
			}

			@Override
			public void onFinish() {
				super.onFinish ();
			}
		});
	}

	@Override
	public void onClick(View v) {
//		Intent intent = new Intent();
		switch (v.getId ()) {
			case R.id.zhongchouinfo_text_morelist:
				intent.setClass (ZhongchouinfoActivity.this, ZhongchouchildActivity.class);
				intent.putExtra ("id", id);
				startActivity (intent);
				break;

			case R.id.zhongchouinfo_text_ok:
				if (myzhong.equals ("2")) {
					shouhuo ();
				} else if (myzhong.equals ("0") && ! type.equals ("3")) {
					intent.setClass (ZhongchouinfoActivity.this, ZhongchouchildActivity.class);
					intent.putExtra ("id", id);
					startActivity (intent);
				} else if (myzhong.equals ("0") && type.equals ("3")) {
					intent.setClass (ZhongchouinfoActivity.this, ZhouchougongyiActivity.class);
					intent.putExtra ("data", jsonObject);
					startActivity (intent);
				}
				break;

			default:
				break;
		}
	}

	private void setSettings(WebSettings settings) {
		settings.setJavaScriptEnabled (true);// ���ÿ�������JS�ű�
		settings.setDefaultTextEncodingName ("UTF-8");
		settings.setLayoutAlgorithm (LayoutAlgorithm.SINGLE_COLUMN);
		mWebView.setBackgroundColor (getResources ().getColor (R.color.white)); // ���ñ���ɫ
//		mWebView.getBackground().setAlpha(50); // �������͸���� ��Χ��0-255 
		settings.setSupportZoom (false);// ��������webview�Ŵ�
		settings.setBuiltInZoomControls (false);
		settings.setDomStorageEnabled (true);
		settings.setTextSize (TextSize.NORMAL);
//		settings.setPluginState(PluginState.ON);
		settings.setDatabaseEnabled (true);
	}

	// jsͨ�Žӿ�
	public class JavascriptInterface {

		private Context context;
		Intent intent = new Intent ();

		public JavascriptInterface(Context context) {
			this.context = context;
		}

		public void thisImage(String index) {
			intent.putExtra ("index", index);
		}
	}

	// ����
	private class MyWebViewClient extends WebViewClient {
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			if (url != null) {
				Uri uri = Uri.parse (url);
				Intent intent = new Intent (Intent.ACTION_VIEW, uri);
				startActivity (intent);

//				finish();
				return true;
			}
//			return false;

			return super.shouldOverrideUrlLoading (view, url);
		}

		@Override
		public void onPageFinished(WebView view, String url) {
			view.getSettings ().setJavaScriptEnabled (true);
			mWebView.setVisibility (View.VISIBLE);
			super.onPageFinished (view, url);
		}

		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			view.getSettings ().setJavaScriptEnabled (true);
			super.onPageStarted (view, url, favicon);
		}

		@Override
		public void onReceivedError(WebView view, int errorCode,
				String description, String failingUrl) {
			super.onReceivedError (null, 1, "访问失败", null);
		}
	}

	private class MyWebChromeClient extends WebChromeClient {
		@Override
		public void onProgressChanged(WebView view, int newProgress) {
			super.onProgressChanged (view, newProgress);
		}

		@Override
		public void onReceivedTitle(WebView view, String title) {
			super.onReceivedTitle (view, title);
		}

		@Override
		public void onReceivedTouchIconUrl(WebView view, String url,
				boolean precomposed) {
			super.onReceivedTouchIconUrl (view, url, precomposed);
		}
	}

}